//
//  HRExtractMoneyViewController.m
//  Hear
//
//  Created by mac on 2018/1/3.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRExtractMoneyViewController.h"
#import "HRBindingPayViewController.h"
#import "HRMoneyViewModel.h"
@interface HRExtractMoneyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *enterButton;
@property (weak, nonatomic) IBOutlet UIButton *bindingButton;
@property(nonatomic, strong)HRMoneyViewModel *viewModel;

@end

@implementation HRExtractMoneyViewController

#pragma mark - Get and Set
- (HRMoneyViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[HRMoneyViewModel alloc] init];
    }
    return _viewModel;
}



- (IBAction)enterClick:(id)sender {
    [self.view endEditing:NO];
    self.enterButton.enabled = NO;
    if (![self.userInfoModel.AlipayName intValue]){
        self.enterButton.enabled = YES;
        [MBProgressHUD showMessage:@"请先绑定支付宝" finishBlock:nil];
        return;
    }
    if (!self.textField.text.length){
        self.enterButton.enabled = YES;
        [MBProgressHUD showMessage:@"提现金额不能为空" finishBlock:nil];
        return;
    }
    if ([self.textField.text intValue] > [self.moneyLabel.text intValue]){
        self.enterButton.enabled = YES;
        [MBProgressHUD showMessage:@"乐币不足" finishBlock:nil];
        return;
    }
    if ([self.textField.text intValue] < 50 || [self.textField.text intValue] > 200){
        self.enterButton.enabled = YES;
        [MBProgressHUD showMessage:@"提现额度最低50乐币最高200乐币" finishBlock:nil];
        return;
    }
    WeakSelf
    
    NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
    paramDict[@"LocalId"] = @"";
    paramDict[@"UserLocalId"] = USERID;
    paramDict[@"Amount"] = self.textField.text;
    paramDict[@"Status"] = @"0";
    paramDict[@"CreateTime"] = [self getCurrentTime];
    paramDict[@"CashTime"] = [self getCurrentTime];
    [self.viewModel postSaveUserCashApplication:paramDict isSeccess:^(BOOL isSeccess) {
        weakSelf.enterButton.enabled = YES;
        if (isSeccess){
            weakSelf.moneyLabel.text = [NSString stringWithFormat:@"%d",[self.moneyLabel.text intValue]-[self.textField.text intValue]];
            weakSelf.textField.text = @"";
            [MBProgressHUD showMessage:@"两个工作内到账" finishBlock:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadIconAndBackGroundNotififation" object:nil];
        }
    }];
    
}
- (IBAction)bindingClick:(id)sender {
    HRBindingPayViewController *VC = [[HRBindingPayViewController alloc] initWithNibName:@"HRBindingPayViewController" bundle:nil];
    VC.title = @"绑定支付宝";
    [self.navigationController pushViewController:VC animated:YES];
    WeakSelf
    VC.bindingBlock = ^(NSString *accontStr){
        [weakSelf.bindingButton setTitle:[NSString stringWithFormat:@"您的支付账号：%@",accontStr] forState:0];
        weakSelf.bindingButton.enabled = NO;
        weakSelf.userInfoModel.AlipayName = @"1";
    };
}

- (NSString *)getCurrentTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    return [formatter stringFromDate:[NSDate date]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    [self setupView];
    // Do any additional setup after loading the view from its nib.
}

- (void)setupView{
    self.userInfoModel.Balance = [[NSUserDefaults standardUserDefaults] objectForKey:@"Balance"];
    self.moneyLabel.text = [self.userInfoModel.Balance intValue]?self.userInfoModel.Balance:@"0";
    
    if ([self.userInfoModel.AlipayName intValue]){
        [self.bindingButton setTitle:[NSString stringWithFormat:@"您的支付账号：%@",self.userInfoModel.AlipayName] forState:0];
        self.bindingButton.enabled = NO;
        NSMutableAttributedString *bindingArrStr = [[NSMutableAttributedString alloc] initWithString:self.bindingButton.titleLabel.text];
        [bindingArrStr setColor:FirstTextColor];
        [bindingArrStr setColor:BlueColor range:NSMakeRange(2, 3)];
        [self.bindingButton setAttributedTitle:bindingArrStr forState:0];
    }else{
        NSMutableAttributedString *bindingArrStr = [[NSMutableAttributedString alloc] initWithString:self.bindingButton.titleLabel.text];
        [bindingArrStr setColor:FirstTextColor];
        [bindingArrStr setColor:BlueColor range:NSMakeRange(4, 3)];
        [bindingArrStr setColor:BlueColor range:NSMakeRange(12, 3)];
        [self.bindingButton setAttributedTitle:bindingArrStr forState:0];
    }

    
    self.enterButton.clipsToBounds = YES;
    self.enterButton.layer.cornerRadius = 8;
    
    self.textField.keyboardType = UIKeyboardTypeNumberPad;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
